package com.getRulersConditions;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GetAll {
	boolean isContainReferenceValues = false;// 是否包含需要的静态设计间隙参考值
	boolean isContainTwoMore = false;// 是否包含两个或两个以上零件号
	int number = 1;// 规则编号

	StringBuffer ruler = new StringBuffer();
	StringBuffer condition = new StringBuffer();
	String left = "";
	String right = "";

	public GetAll(int number) {
		this.number = number;
	}

	public void writeRuler(String ruler1) throws IOException {
		// 判断文件是否存在
		String rulerpath = "E:\\rules.csvcldb";
		File filename = new File(rulerpath);
		if (filename.exists())
			filename.delete();
		filename.createNewFile();
		String title = "Rule Number,Product Name,Rule Description,Requirement";// 规则标题
		PrintWriter pw = new PrintWriter(new FileOutputStream(filename));
		pw.println(title);
		pw.flush();
		pw.println(ruler1);
		pw.flush();
		pw.close();
	}

	public void writeCondition(String condition1) throws IOException {
		// 判断文件是否存在
		String rulerpath = "E:\\conditions.csvcldb";
		File filename = new File(rulerpath);
		if (filename.exists())
			filename.delete();
		filename.createNewFile();
		String title = "Rule Number,Condition Description,Condition Domain,Search Type,Match Type,Attribute Name,Attribute Pattern";// 内容标题
		PrintWriter pw = new PrintWriter(new FileOutputStream(filename));
		pw.println(title);
		pw.flush();
		pw.println(condition1);
		pw.flush();
		pw.close();
	}

	public void prt() throws IOException {
		writeRuler(ruler.toString());
		writeCondition(condition.toString());
	}

	public void getRulersConditions(String line) throws IOException {
		String[] PartNumber = {};// 每行的零件号数组
		// double ReferenceValues = 1.0;// 静态设计间隙参考值
		isContainReferenceValues = false;// 判断前置为false
		isContainTwoMore = false;// 判断前置为false

		Double[] ReferenceValues = getRequirementArry(line);
		int length = ReferenceValues.length;
		PartNumber = getID(line);
		if (isContainReferenceValues && isContainTwoMore) {
			for (int i = 0; i < length; i++) {
				combination(number, PartNumber, 2, ReferenceValues[i]);
			}
		}
	}

	public Double[] getRequirementArry(String coment) {
		String reg = "\\≥\\d+(\\.\\d*)?";// 一、\\≥\\d+(\\.\\d*)?   二、\\≥[\\d+]+(\\.\\d*)?
		Pattern pattern = Pattern.compile(reg);
		Matcher m = pattern.matcher(coment);
		isContainReferenceValues = m.find();
		boolean isContain = isContainReferenceValues;
		int i = 0;
		List list = new ArrayList();
		while (isContain) {
			i++;
			String value = m.group();
			list.add(value);
			isContain = m.find();
		}
		int size = list.size();
		String temp = "";
		Double[] array2 = new Double[size];
		for (int x = 0; x < list.size(); x++) {
			temp = ((String) list.get(x)).replaceAll("≥", "");
			array2[x] = Double.parseDouble(temp);
		}
		return array2;
	}

	// 获得静态设计间隙参考值
	public double getRequirement(String coment) {
		Double i = 1.0;
		String reg = "\\≥[\\d+]+(\\.\\d*)?";
		Pattern pattern = Pattern.compile(reg);
		Matcher m = pattern.matcher(coment);
		isContainReferenceValues = m.find();
		if (isContainReferenceValues) {
			String value = m.group();
			value = value.replaceAll("≥", "");
			i = Double.parseDouble(value);
		}
		return i;
	}

	// 需要检查的零件号
	public String[] getID(String coment) {
		String reg = "\\w+\\-\\w+(\\-\\w+)*";// 
		Pattern pattern = Pattern.compile(reg);
		Matcher m = pattern.matcher(coment);
		boolean result = m.find();
		int i = 0;
		List list = new ArrayList();
		while (result) {
			i++;
			String value = m.group();
			list.add(value);
			result = m.find();
		}
		int size = list.size();
		if (size >= 2) {
			isContainTwoMore = true;
		}
		String[] array = new String[size];
		for (int x = 0; x < list.size(); x++) {
			array[x] = (String) list.get(x);
		}
		return array;
	}

	// 下面是规则、条件生成逻辑逻辑
	public void combination(int rulernumber, String[] ia, int n, double dayu)
			throws IOException {
		number = rulernumber;
		double number1 = formatnumber(dayu);

		combination(rulernumber, "", ia, n, number1);
	}

	public double formatnumber(double dayu) {
		double f = dayu / 1000.0;
		BigDecimal bg = new BigDecimal(f);
		double f1 = bg.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
		return f1;
	}

	public void combination(int rulernumber, String s, String[] ia, int n,
			double dayu) throws IOException {
		if (n == 1) {
			for (int i = 0; i < ia.length; i++) {
				left = number + ",\"a\",Left Node,Node Parents,Y,eaiclNAME,%"
						+ s + "%";
				right = number + ",\"a\",Right Node,Node Parents,Y,eaiclNAME,%"
						+ ia[i] + "%";
				condition.append(left + "\r\n" + right + "\r\n");
				ruler.append(number + ",ProductName,\"RuleDescription\","
						+ dayu + "\r\n");
				number++;
			}
		} else {
			for (int i = 0; i < ia.length - (n - 1); i++) {
				String ss = "";
				ss = s + ia[i];
				String[] ii = new String[ia.length - i - 1];
				for (int j = 0; j < ia.length - i - 1; j++) {
					ii[j] = ia[i + j + 1];
				}
				combination(rulernumber, ss, ii, n - 1, dayu);
			}
		}
	}
}
